<?php
/*
* $Author ：PHPYUN开发团队
*
* 官网: http://www.phpyun.com
*
* 版权所有 2009-2018 宿迁鑫潮信息技术有限公司，并保留所有权利。
*
* 软件声明：未经授权前提下，不得用于商业运营、二次开发以及任何形式的再次发布。
 */

 
class adminCommon extends common{
 	function admin(){
		$r=$this->get_admin_user_shell();

 		if($this->config['sy_iscsrf']!='2'){
			if(!$_SESSION['pytoken']){
				$_SESSION['pytoken'] = substr(md5(uniqid().$_SESSION['auid'].$_SESSION['ausername'].$_SESSION['ashell']), 8, 12);
			}
			if($_POST){
//				if($_POST['pytoken']!=$_SESSION['pytoken']){
//					$this->ACT_layer_msg("来源地址非法！",8,$this->config['sy_weburl']);
//				}
				unset($_POST['pytoken']);
			}
			$this->yunset('pytoken',$_SESSION['pytoken']);
		}
  }

   function wapadmin(){
		$UA = strtoupper($_SERVER['HTTP_USER_AGENT']);
		if(strpos($UA, 'WINDOWS NT') !== false){
 		}
		
		if($_SESSION['wuid'] || $_GET['c']){
			$shell = $this->admin_get_user_shell($_SESSION['wuid'],$_SESSION['wshell']);

			if(!is_array($shell)){
				unset($_SESSION['authcode']);
				unset($_SESSION['wuid']);
				unset($_SESSION['wusername']);
				unset($_SESSION['wshell']);
				unset($_SESSION['md']);
				unset($_SESSION['tooken']);
				unset($_SESSION['xsstooken']);
				header("location:".$this->config['sy_weburl'].'/wapadmin/');
				exit();
			}
		}
	}
  
  function get_admin_user_shell(){
			$row=$this->admin_get_user_shell();
			if(!$row){
        	$this->adminlogout();
//        exit("登录超时，请刷新后重新登录！");
			}

			if($_GET['m']=="" || $_GET['m']=="index" || $_GET['m']=="ajax" || $_GET['m']=="admin_nav"){$_GET['m']="admin_right";}
			if($this->config['did']&&$_GET['a']){
				$_GET['c']=$_GET['a'];
			}
			$c=$_GET['c'];
			$m=$_GET['m'];
			if($_GET['m']!="admin_right"){
 				$url="index.php?m=".$m;
				$c_array=array("cache","markcom","markuser","advertise","zhaopinhui","admin_user","wx","admin_siteadmin");
				if($c && $c!='savagroup'&& in_array($m,$c_array)){
					if($m=='admin_user' && $c == 'pass'){
						$c ='myuser';
					}
					$url.="&c=".$c;
                    $info=$this->obj->DB_select_once("admin_navigation","`url`='".$url."'");
					if(empty($info)){
						$url="index.php?m=".$m;
					}
				}

//				$nav=$this->get_shell($row["m_id"],$url);
//				if(!$nav){
// 				  echo "登录超时，请刷新后重新登录！";
//				  exit();
//				}

				if(is_numeric($this->config['did'])){
				  if($m=="admin_user"){
					$where="(`url`='index.php?m=admin_user&c=pass' or `url`='index.php?m=admin_user&c=myuser') and `dids`=1";
				  }else{
					$where="`url`='".$url."' and `dids`=1";
				  }
				  $info=$this->obj->DB_select_once("admin_navigation",$where);
				}else{
							$info=$this->obj->DB_select_once("admin_navigation","`url`='".$url."'");
				}
				
//				if(!$info){
//				  echo "登录超时，请刷新后重新登录！";
//							exit();
//				}
			}
  }

 	function adminlogout(){
		unset($_SESSION['authcode']);
		unset($_SESSION['auid']);
		unset($_SESSION['ausername']);
		unset($_SESSION['ashell']);
		unset($_SESSION['md']);
		unset($_SESSION['tooken']);
		unset($_SESSION['xsstooken']);
	}
  
 
	function admin_get_user_login($username,$password,$url='index.php') {
		global $config;
		$username = str_replace(" ", "", $username);
		if($config['sy_web_site']=='1'){
			$query = $this->db->query("SELECT * FROM `".$this->def."admin_user` WHERE `username`='$username' and (`did`='".$config['did']."' or `isdid`='1') and `status` = 1 limit 1");
		}else{
			$query = $this->db->query("SELECT * FROM `".$this->def."admin_user` WHERE `username`='$username' and `status` = 1 limit 1");
		}

		$us = is_array($row = $this->db->fetch_array($query));
		$ps = $us ? md5($password) == $row['password'] : FALSE;
		
		if($ps){
			$this->cookie->setCookie("lasttime",$row['lasttime'],time()+ 80000);
			$_SESSION['auid']=$row['uid'];
			$_SESSION['ausername']=$row['username'];
			$_SESSION['xsstooken'] = sha1($config['sy_safekey']);
			$_SESSION['ashell']=md5($row['username'] . $row['password'] . $this->md);
			$this->cookie->setCookie("ashell", md5($row['username'] . $row['password'] . $this->md), time() + 80000);
			$this->obj->DB_update_all("admin_user","`lasttime`='".time()."'","`uid`='".$row['uid']."'");
			$this->ACT_layer_msg("登录成功！",9,$url);
			
		} else {
			$this->ACT_layer_msg("密码或用户错误！",8,$url);
		}
	}

   
	function wapadmin_get_user_login($username,$password,$url='index.php') {
		global $config;
		$username = str_replace(" ", "", $username);
		if($config['sy_web_site']=='1'){
			$query = $this->db->query("SELECT * FROM `".$this->def."admin_user` WHERE `username`='$username' and (`did`='".$config['did']."' or `isdid`='1') limit 1");
		}else{
			$query = $this->db->query("SELECT * FROM `".$this->def."admin_user` WHERE `username`='$username' limit 1");
		}

		$us = is_array($row = $this->db->fetch_array($query));
		$ps = $us ? md5($password) == $row['password'] : FALSE;
		
		if($ps){

			$_SESSION['wuid']=$row['uid'];
			$_SESSION['wusername']=$row['username'];
			$_SESSION['xsstooken'] = sha1($config['sy_safekey']);
			$_SESSION['wshell']=md5($row['username'] . $row['password'] . $this->md);
			$this->cookie->setCookie("wshell", md5($row['username'] . $row['password'] . $this->md), time() + 80000);
			$this->obj->DB_update_all("admin_user","`lasttime`='".time()."'","`uid`='".$row['uid']."'");
			return true;
			
		} else {
			return false;
		}
  }
   
	function admin_get_user_shell($uid = '',$shell = true){
        global $config;
//        if(!preg_match("/^\d*$/",$uid)){return false;}
//        if($config['sy_web_site']=='1'){
//          $query = $this->db->query("SELECT username,password FROM `".$this->def."admin_user` WHERE `uid`='$uid' and (`did`='".$config['did']."' or `isdid`='1') limit 1");
//        }else{
//          $query = $this->db->query("SELECT username,password FROM `".$this->def."admin_user` WHERE `uid`='$uid'  limit 1");
//        }
//        $us = is_array($row = $this->db->fetch_array($query));
		//java访问验证对比

		$row = array();
		$row['m_id'] = 1;
		$row['username'] = 'admin';
		$row['password'] = 'e10adc3949ba59abbe56e057f20f883e';
		return $shell ? $row : NULL;
    }
    
    function logo_reset($name,$value){
        $config = $this->obj->DB_select_once("admi_config","`name`='$name'");
        if (!$config){
            $this->obj->DB_insert_once("admin_config","`config`='$value',`name`='$name'");
        }elseif ($config['config'] != $value){
            $logo = $this->checksrc($value,$config['config']);
            $this->obj->DB_update_all("admin_config","`config`='$logo'","`name`='$name'");
        }
    }

    //获取统计数据
    function getEverything( $type ){
        $res = array();
        $res['date'] = [1,2,3,4,5,6,7,8,9,10,11,12];
        $year = date('Y',time());
        $month_data = array();
        //设置12个月的时间戳
        for ( $i = 1; $i <= 12; $i++){
            $arr[] = strtotime($year.'-'.$i.'-01');
        }
        $next_year = $year+1;
        $arr[] = strtotime($next_year.'-01-01');

        //循环业务类型
        switch ( $type ){
            case 'zhaopin':
                //总数据
                $num_id=$this->obj->DB_select_all('company_job',"1 ",'`id`,COUNT(id) as num');
                $num_jobhits=$this->obj->DB_select_all('company_job',"1 ",'`jobhits`,SUM(jobhits) as num');
                $num_entry=$this->obj->DB_select_all('entry',"`entry_state` = '3'",'`id`,COUNT(id) as num');
                $res['num_id'] = $num_id[0]['num'];
                $res['num_jobhits'] = $num_jobhits[0]['num'];
                $res['num_entry'] = $num_entry[0]['num'];
                //获取每月数据
                foreach ( $res['date'] as $k => $v ){
                    $time = $arr[$k+1]-1;
                    $month_data=$this->obj->DB_select_all('company_job',"`sdate` BETWEEN '".$arr[$k]."' AND '".$time."'",'`id`,COUNT(id) as num');
                    $res['month_data'][] = $month_data[0]['num'];
                }
                break;
            case 'ruzhi':
                //总数据
                $num_id=$this->obj->DB_select_all('entry',"1 ",'`id`,COUNT(id) as num');
                $num_entry=$this->obj->DB_select_all('entry',"`entry_state` = '3'",'`id`,COUNT(id) as num');
                $num_entrying=$this->obj->DB_select_all('entry',"`entry_state` = '1'",'`id`,COUNT(id) as num');
                $res['num_id'] = $num_id[0]['num'];
                $res['num_entry'] = $num_entry[0]['num'];
                $res['num_entrying'] = $num_entrying[0]['num'];
                //获取每月数据(统计已入职人数)
                foreach ( $res['date'] as $k => $v ){
                    $time = $arr[$k+1]-1;
                    $month_data=$this->obj->DB_select_all('entry',"`entry_state` = '3' AND `add_time` BETWEEN '".$arr[$k]."' AND '".$time."'",'`id`,COUNT(id) as num');
                    $res['month_data'][] = $month_data[0]['num'];
                }
                break;
            case 'jifen':
                //总数据
                $time_start = strtotime(date('Y-m-d',time()));
                $time_end = strtotime(date('Y-m-d',time()+86400));
                $integral=$this->obj->DB_select_all('member_statis',"1 ",'`integral`,SUM(integral) as num');
                $order=$this->obj->DB_select_all('change',"`ctime` BETWEEN '".$time_start."' AND '".$time_end."'",'`id`,COUNT(id) as num');
                $goods=$this->obj->DB_select_all('reward',"`sdate` BETWEEN '".$time_start."' AND '".$time_end."'",'`id`,COUNT(id) as num');
                $all_goods=$this->obj->DB_select_all('reward',"1 ",'`id`,COUNT(id) as num');
                $goods_up=$this->obj->DB_select_all('reward',"`status` = '1'",'`id`,COUNT(id) as num');
                $goods_down=$this->obj->DB_select_all('reward',"`status` = '2'",'`id`,COUNT(id) as num');
                $res['integral'] = $integral[0]['num'];
                $res['order'] = $order[0]['num'];
                $res['goods'] = $goods[0]['num'];
                $res['desc_list'] = ['总商品数','已上架商品数','已下架商品数'];
                $res['value_list'] = [$all_goods[0]['num'],$goods_up[0]['num'],$goods_down[0]['num']];
                unset($res['date']);
                break;
        }
        if( $type != 'jifen' ){
            $res['date'] = ['1月','2月','3月','4月','5月','6月','7月','8月','9月','10月','11月','12月'];
        }
        return $res;
    }
}
?>